package com.dyzx.hbb.common.config;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.dyzx.hbb.common.constant.InitConfig;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;


@Slf4j
@Configuration
public class AppConfig {
    @Value(value = "${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value(value = "${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value(value = "${aliyun.oss.endpoint}")
    private String endpoint;
    @Value(value = "${aliyun.oss.bucket}")
    private String bucket;
    @Value(value = "${aliyun.oss.region}")
    private String region;

    @Value(value = "${aliyun.oss.host}")
    private String host;
 @PostConstruct
    public void serviceA() {
    log.info("serviceA init");
    }
    @Bean
    public OSS aliyunOSSClient() {
        InitConfig.setPathUrl(host);

        // 创建 CredentialsProvider
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);

        // 创建OSSClient实例。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        return OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();
    }
}